import {Component, OnInit} from '@angular/core';
import {Banner} from '../../module/banner';
import {HttpService} from '../../service/http.service';
import {ProjectRank} from '../../module/projectRank';
import {HotSearch} from '../../module/hotSearch';
import {HeatProject} from '../../module/HeatProject';
import {BusinessRank} from '../../module/businessRank';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  heatProjectEndId = 0;
  sliderList: Banner[];
  businessRankList: BusinessRank[];
  projectRankList: ProjectRank[];
  hotSearchList: HotSearch[];
  heatProjectList: HeatProject[];

  constructor(private httpService: HttpService) {
  };

  ngOnInit() {
    // Banner轮播
    this.httpService.getBannerList().then(r => this.sliderList = r);
    // 商机排行榜
    this.httpService.getBusinessRankList().then(r => this.businessRankList = r);
    // 大家都在找
    this.httpService.getHotSearchList().then((r => this.hotSearchList = r));
    // 热门品牌
    this.getMoreHeatProject();
    // 品牌榜中榜
    this.httpService.getProjectRank().then(r => {
      for (let i = 0; i < r.length; i++) {
        const buff: string[] = (r[i].project.major).split(' ');
        for (let j = 0; j < buff.length; j++) {
          if (buff[j].length < 2) {
            buff.splice(j, 1);
          }
        }
        r[i].project.majors = buff;
      }
      this.projectRankList = r.slice(0, 6);
    });
  }

  changeHeatProjectEndId(id) {
    this.heatProjectEndId = id;
  }

  getMoreHeatProject() {
    // 热门品牌
    this.httpService.getHeatProjectList(this.heatProjectEndId).then((r => {
      if (r.length > 0) {
        this.changeHeatProjectEndId(r[(r.length - 1)].id);
        this.heatProjectList = r;
      } else {
        this.changeHeatProjectEndId(0);
      }
    }));
  }
}
